package com.xr.miniprogram.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xr.common.constant.MiniProgramConstants;
import com.xr.miniprogram.domain.MiniProgramComment;
import com.xr.miniprogram.domain.MiniProgramStandards;
import com.xr.miniprogram.domain.vo.MiniProgramSearchVO;
import com.xr.miniprogram.domain.vo.MiniProgramStandardsVO;
import com.xr.miniprogram.mapper.MiniProgramCommentMapper;
import com.xr.miniprogram.mapper.MiniProgramStandardsMapper;
import com.xr.miniprogram.service.IMiniProgramStandardsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class IMiniProgramStandardsServiceImpl extends ServiceImpl<MiniProgramStandardsMapper, MiniProgramStandards> implements IMiniProgramStandardsService {


    @Resource
    MiniProgramStandardsMapper standardsMapper;

    @Resource
    MiniProgramCommentMapper commentMapper;
    @Override
    public List<MiniProgramStandards> getStandardsListByServiceId(Long serviceId) {
        return this.list(new QueryWrapper<MiniProgramStandards>().lambda().eq(MiniProgramStandards::getServiceId, serviceId));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteStandards(Long serviceStandardsId) {
        return this.removeById(serviceStandardsId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addStandards(MiniProgramStandards miniProgramStandards) {
        return this.save(miniProgramStandards);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateStandards(MiniProgramStandards miniProgramStandards) {
        return this.updateById(miniProgramStandards);
    }

    @Override
    public List<MiniProgramSearchVO> searchAll(String keyword, Long topic) {
        //1代表默认即按照销量排序，2代表按好评率排序，3代表按照折扣率排子查询序，折扣率越低越靠前
        List<MiniProgramSearchVO> searchVOList = standardsMapper.getByKeywordAndTopic(keyword, topic);
        for (MiniProgramSearchVO searchVO : searchVOList) {//查询销量
            searchVO.setStandardsSum(standardsMapper.countStandardOrder(searchVO.getServiceStandardsId()));
        }

        if (topic.equals(MiniProgramConstants.SEARCH_TOPIC_1)) searchVOList = searchVOList.stream() //销量排序
                    .sorted(Comparator.comparing(MiniProgramSearchVO::getStandardsSum).reversed())
                    .collect(Collectors.toList());
        if (topic.equals(MiniProgramConstants.SEARCH_TOPIC_2)){//好评率排序
            // standardsId --> commentsList --> rate --> sort
            for (MiniProgramSearchVO miniProgramSearchVO : searchVOList) {
                List<MiniProgramComment> commentList = commentMapper.selectList(new QueryWrapper<MiniProgramComment>()
                        .lambda().eq(MiniProgramComment::getStandardsId, miniProgramSearchVO.getServiceStandardsId()));
                int i = 0;
                for (MiniProgramComment miniProgramComment : commentList) {
                    if (miniProgramComment.getRate() > MiniProgramConstants.COMMENT_STANDARD) ++i;
                }
                miniProgramSearchVO.setGoodSum((long) i);
            }
            searchVOList.sort((o1, o2) -> Long.compare(o2.getGoodSum(), o1.getGoodSum()));
        }
        return searchVOList;
    }
}
